package property;

import com.gitee.feizns.dynamic.StringUtils;
import com.gitee.feizns.dynamic.bean.Convertible;
import com.gitee.feizns.dynamic.bean.TryGet;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * @author feizns
 * @since 2019/6/16 0016
 */
public class ConvertibleDemo {

    public static void main(String[] args) {

        User user = new User(A.firstName, "lastName");
        //从其他实体上获取属性值
        Admin admin = new Admin();
        admin.from(user);
        System.out.println(admin);
        //从map上获取属性值
        Map<String, Object> map = new HashMap<>();
        map.put("firstName", A.firstName);
        map.put("lastName", "ll");
        admin.from(map);
        System.out.println(admin);

        User user1 = user.copyPropertiesTo(new User());

        System.out.println(user1);   //ConvertibleDemo.User(firstName=firstName, lastName=lastName)

        //注解定义映射规则
        Admin to1 = user.to(Admin.class);

        System.out.println(to1);     //ConvertibleDemo.Admin(newLastName=lastName, first_name=firstName, last_name=lastName)

        //自定义映射规则
        Admin to2 = user.copyPropertiesTo(Admin.class, StringUtils::toUnderline);

        System.out.println(to2);     //ConvertibleDemo.Admin(newLastName=null, first_name=firstName, last_name=lastName)

    }

    enum A {

        firstName(1);

        private int val;

        A(int val) {
            this.val = val;
        }

    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class User implements Convertible {

        private A firstName;

        private String lastName;

    }

    @Data
    public static class Admin implements Convertible {

        @TryGet("lastName")
        private String newLastName;

        @TryGet("firstName")
        private String first_name;

        @TryGet("lastName")
        private String last_name;

    }

}
